package edu.neu.swc.commerce.stream;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.commerce.vo.QinyiMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

/**
 * <h1>使用默认通信信道实现消息发送</h1>
 */
@Slf4j
@EnableBinding(Source.class)
public class DefaultSendService {

    private final Source source;

    public DefaultSendService(Source source) {
        this.source = source;
    }

    /**
     * <h2>使用默认的输出信道发送消息</h2>
     */
    public void sendMessage(QinyiMessage qinyiMessage){
        String _message = JSON.toJSONString(qinyiMessage);

        log.info("in DefaultSendService send message:[{}]", _message);

        // SpringCloud Stream 基于 Message 模块，统一消息的编程模型，是 Stream 的重要组成部分之一
        source.output().send(MessageBuilder.withPayload(_message).build());
    }
}
